package com.bamboo.tool.template.torna.util;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.torna.sdk.client.OpenClient;
import com.alibaba.fastjson.JSONObject;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.template.torna.entity.*;
import com.bamboo.tool.util.RequestUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bamboo
 * Date 2022/9/20 16:32
 * Description
 */
public class TornaRequestUtil {

    public static OpenClient client = null;

    /**
     * Torna 登录接口
     *
     * @param tornaSetting Torna 配置
     */
    public static TornaRespBase<LoginToken> login(TornaSetting tornaSetting) {
        String loginUrl = RequestUtil.getUrl(tornaSetting.getUrl(), "system/login");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("username", tornaSetting.getUserName());
        paramMap.put("password", SecureUtil.md5(tornaSetting.getPassword()));
        String resp = HttpUtil.post(loginUrl, JSONUtil.toJsonStr(paramMap), 2000);
        TornaRespBase tornaRespBase = JSONObject.parseObject(resp, TornaRespBase.class);
        if (tornaRespBase.isOk()) {
            LoginToken loginToken = JSONObject.toJavaObject((JSONObject) tornaRespBase.getData(), LoginToken.class);
            tornaRespBase.setData(loginToken);
        }

        return (TornaRespBase<LoginToken>) tornaRespBase;
    }

    /**
     * 获取用户的空间
     *
     * @return 用户的空间列表
     */
    public static TornaRespBase<List<SpaceDTO>> getSpaceList() {
        TornaRespBase tornaResp = getTornaResp("space/list");
        if (tornaResp.isOk()) {
            List<SpaceDTO> spaceDTOS = JSONObject.parseArray(JSONObject.toJSONString(tornaResp.getData()), SpaceDTO.class);
            tornaResp.setData(spaceDTOS);
        }
        return (TornaRespBase<List<SpaceDTO>>) tornaResp;
    }


    /**
     * 获取模块token
     *
     * @return 用户的空间列表
     */
    public static TornaRespBase<String> getModelToken(String moduleId) {
        TornaRespBase tornaResp = getTornaResp("module/token/get?moduleId=" + moduleId);
        return (TornaRespBase<String>) tornaResp;
    }

    /**
     * 获取空间下得项目列表
     *
     * @param spaceId 空间id
     * @return 项目列表
     */
    public static TornaRespBase<List<ProjectDTO>> getSpaceProjectList(String spaceId) {
        TornaRespBase tornaResp = getTornaResp("space/project/list?spaceId=" + spaceId);
        if (tornaResp.isOk()) {
            List<ProjectDTO> spaceDTOS = JSONObject.parseArray(JSONObject.toJSONString(tornaResp.getData()), ProjectDTO.class);
            tornaResp.setData(spaceDTOS);
        }
        return (TornaRespBase<List<ProjectDTO>>) tornaResp;
    }


    /**
     * 获取项目下得模块列表
     *
     * @param projectId 项目
     * @return 模块列表
     */
    public static TornaRespBase<List<ModuleVO>> getProjectModelList(String projectId) {
        TornaRespBase tornaResp = getTornaResp("module/list?projectId=" + projectId);
        if (tornaResp.isOk()) {
            List<ModuleVO> spaceDTOS = JSONObject.parseArray(JSONObject.toJSONString(tornaResp.getData()), ModuleVO.class);
            tornaResp.setData(spaceDTOS);
        }
        return (TornaRespBase<List<ModuleVO>>) tornaResp;
    }



    public static TornaRespBase getTornaResp(String url) {
        TornaSetting state = RestfulCloudApplicationCache.getInstance().getState().getTornaSetting();
        String loginUrl = RequestUtil.getUrl(state.getUrl(), url);
        HttpRequest get = HttpUtil.createGet(loginUrl);
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Bearer " + state.getToken());
        get.addHeaders(headers);
        HttpResponse execute = get.execute();
        TornaRespBase tornaRespBase = JSONObject.
                parseObject(execute.body(), TornaRespBase.class);
        return tornaRespBase;
    }

}
